/*
 * Copyright (c) 2020 Fraunhofer FOKUS and others. All rights reserved.
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contact: mosaic@fokus.fraunhofer.de
 */
package org.eclipse.mosaic.fed.carla.clojure.commands;

import libpython_clj2.java_api;
import org.eclipse.mosaic.fed.carla.clojure.AbstractCarlaCommand;
import org.eclipse.mosaic.fed.carla.clojure.ClojureCarlaConnection;
import org.eclipse.mosaic.lib.objects.vehicle.VehicleData;

public class VehicleAdd extends AbstractCarlaCommand {

    public VehicleAdd() {
        super();
    }

    public void execute(ClojureCarlaConnection connection, VehicleData sumoVehicleData) {
        try (AutoCloseable locker = java_api.GILLocker()) {
            Object carlaBlueprint = getCarlaBlueprint(sumoVehicleData);
            Object carlaTransform = getCarlaTransform(connection, sumoVehicleData);
            long carlaActorId = spawnerCarlaActor(connection, carlaBlueprint, carlaTransform);
            connection.sumo2carlaIds.put(sumoVehicleData.getName(), carlaActorId);
        } catch (Exception e) {
            throw new RuntimeException("Vehicle add failed when request carla. ", e);
        }
    }

}
